﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lenovo.CFI.BLL;
using Lenovo.CFI.BLL.DicMgr;
using Lenovo.CFI.BLL.Sys;
using Lenovo.CFI.Common;
using Lenovo.CFI.Common.Dic;
using Lenovo.CFI.Common.Sys;
using Lenovo.CFI.Web.Helper;

namespace Lenovo.CFI.Web.VP
{
    public partial class MyIdeaNew : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int? topicID = UrlHelper.GetQueryStringToInt("tid");
                Topic topic = null;
                if (topicID.HasValue) topic = new TopicBl().GetTopicByID(topicID.Value);
                if (topic == null) ErrorHelper.RaiseNoValueError();

                if (topic.Status != TopicStatus.Start ||
                    DateTime.Now < topic.StartTime)
                    ErrorHelper.RaiseError("The topic does not start or has been closed!");

                this.TxtTopic.Text = topic.Title;
                ViewState["TopicID"] = topic.ID;


                BindHelper.BindIdeaType(this.DdlIdeaType, "Select...", "-1", null, false);
                BindHelper.BindIdeaType(this.CbRelatedType, null, null, null, false);

                BindHelper.BindKeyword(DdlKeyword1, "Select...", "-1", null, false);
                BindHelper.BindKeyword(DdlKeyword2, "Select...", "-1", null, false);
                BindHelper.BindKeyword(DdlKeyword3, "Select...", "-1", null, false);

                this.AfuFilePpt.SetFile(null, null, null);
                this.AfuFilePic1.SetFile(null, null, null);
                this.AfuFilePic2.SetFile(null, null, null);
                this.AfuFilePic3.SetFile(null, null, null);


                BindHelper.BindUser(this.DdlApprover, "Select...", "-1", null, UserHelper.TeamID);

                this.TxtOwner.Text = UserHelper.UserName;
            }
        }

        protected void BtnSaveAndFinish_Click(object sender, EventArgs e)
        {
            try
            {
                this.Save(true);
                MessageHelper.Show("Success!", this.Page);
            }
            catch (ApplicationException ex)
            {
                MessageHelper.RegShowJS(ex.Message, this.Page);
            }

            this.BtnSaveAndFinish.Enabled = false;
            this.BtnSaveDraft.Enabled = false;
        }

        protected void BtnSaveDraft_Click(object sender, EventArgs e)
        {
            try
            {
                this.Save(false);
                MessageHelper.Show("Success!", this.Page);
            }
            catch (ApplicationException ex)
            {
                MessageHelper.RegShowJS(ex.Message, this.Page);
            }

            this.BtnSaveAndFinish.Enabled = false;
            this.BtnSaveDraft.Enabled = false;
        }


        private Idea Save(bool submit)
        {
            Idea idea = new Idea();
            idea.TopicID = Convert.ToInt32((int)ViewState["TopicID"]);
            idea.Type = this.DdlIdeaType.SelectedValue;
            idea.Title = this.TxtTitle.Text.Trim();
            idea.Backgroud = this.TxtBackground.Text.Trim();
            idea.Description = this.TxtDescription.Text.Trim();
            idea.Values = this.TxtValue.Text.Trim();
            idea.Owner = UserHelper.UserName;

            List<string> relatedTypes = new List<string>();
            foreach (ListItem item in this.CbRelatedType.Items)
            {
                if (item.Selected)
                    relatedTypes.Add(item.Value);
            }
            idea.RelatedType = CodeList.CreateByList(relatedTypes.ToArray());

            List<string> keywords = new List<string>();
            if (this.DdlKeyword1.SelectedValue != "-1")
                keywords.Add(this.DdlKeyword1.SelectedValue);
            if (this.DdlKeyword2.SelectedValue != "-1")
                keywords.Add(this.DdlKeyword2.SelectedValue);
            if (this.DdlKeyword3.SelectedValue != "-1")
                keywords.Add(this.DdlKeyword3.SelectedValue);
            idea.Keywords = CodeList.CreateByList(keywords.ToArray());

            if (!string.IsNullOrEmpty(TxtFreeKeyword.Text.Trim()))
                idea.Freewords = new CodeList(TxtFreeKeyword.Text.Trim());

            idea.PptFileID = this.AfuFilePpt.FinalFileID;
            idea.Pic1FileID = this.AfuFilePic1.FinalFileID;
            idea.Pic2FileID = this.AfuFilePic2.FinalFileID;
            idea.Pic3FileID = this.AfuFilePic3.FinalFileID;

            if (this.DdlApprover.SelectedIndex != 0)
                idea.Approver = this.DdlApprover.SelectedValue;

            new IdeaBl().Add(idea, submit);

            return idea;
        }
    }
}